﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;


namespace StudentWeb.Api.Controllers
{
    public class XhEventController : ApiController
    {

        XhEventBO _xheventBO = new XhEventBO();
        #region 活动信息 分页列表
        /// <summary>
        /// 活动信息 分页列表
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        //[ValidToken]
        public IHttpActionResult GetXhEventList(string token, [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                int pageIndex = model.page_index ?? 0;
                int pageSize = model.page_size ?? 0;
                if (pageIndex < 1 || pageSize < 1) return Json(new Result(ResultCode.Fail, "参数错误"));
                var wc = _GetXhEventListWhereC(model.condidate);
                var result = _xheventBO.GetXhEventList(pageIndex, pageSize, wc);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        private string _GetXhEventListWhereC(dynamic qc)
        {
            var wc = "XhEventID>0";
            if (qc == null) return wc;
            string tmp = "";
            
            tmp = ConvertHelper.DynamicToString(qc.xheventName);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and XhEventName like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.xhlocation);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and XhLocation like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.xheventDate);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and XhEventDate like '%{tmp}%'"; }

            return wc;
        }
        #endregion
        #region 活动信息 单条记录
        /// <summary>
        /// 图书信息 单条记录
        /// </summary>
        /// <param name="token"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        [ValidToken]
        public IHttpActionResult GetXhEventInfo(string token, int xheventID)
        {
            try
            {
                var result = _xheventBO.GetXhEventInfo(xheventID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 活动信息 删除
        [HttpPost]
        public IHttpActionResult DeleteEventInfo(int XhEventID)
        {
            try
            {
                if (XhEventID != null)
                {
                    var result = _xheventBO.DeleteEventInfo(XhEventID);
                    return Json(new Result(ResultCode.Fail, "删除成功"));
                }
                else
                {
                    return Json(new Result(ResultCode.Fail, "缺少参数"));
                }
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 活动信息 保存
        /// <summary>
        /// 学生信息 保存
        /// </summary>
        /// <param name="token"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidToken]
        public IHttpActionResult SaveXhEventInfo(string token, [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var user = new TeacherBO().GetTeacherInfo(token);
                var result = _xheventBO.SaveXhEventInfo(model, user.TeacherDataID);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        
    }

}

